/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.item.etcitem.action;

import com.lineage.server.PacketCreate;
import com.lineage.server.codes.ActionCodes;
import com.lineage.server.model.L1PcInventory;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.item.ItemExecutor;
import com.lineage.server.packetserver.S_AttackPacket;
import com.lineage.server.utils.L1SpawnUtil;
import com.lineage.server.utils.Random;

/**
 * Class <code>WandSummonMonster</code> 召唤怪物魔杖.<br>
 * 40006 - 松木魔杖 <br>
 * 40412 - 黑暗安特的树枝 <br>
 * 140006 - 受祝福的 松木魔杖 <br>
 * 
 * @author jrwz
 * @version 2012-6-23上午09:59:18
 * @see com.lineage.server.model.item.etcitem.action
 * @since JDK1.7
 */
public final class WandSummonMonster extends ItemExecutor {

    /**
     * 取得实例.
     * 
     * @return 实例
     */
    public static ItemExecutor get() {
        return new WandSummonMonster();
    }

    /** 召唤怪物魔杖. */
    private WandSummonMonster() {
    }

    @Override
    public void execute(final L1PcInstance pc, final L1ItemInstance useItem,
            final int readC1, final int readC2, final int targetId,
            final int targetX, final int targetY) {
        // 可使用魔杖的地图
        if (pc.getMap().isUsePainwand()) {
            final S_AttackPacket s_attackPacket = new S_AttackPacket(pc, 0,
                    ActionCodes.ACTION_WAND);
            pc.sendPackets(s_attackPacket);
            pc.broadcastPacket(s_attackPacket);
            final int[] mobArray = { 45008, 45140, 45016, 45021, 45025, // 哥布林・哈柏哥布林・鹿・葛林
                    45033, 45099, 45147, 45123, // 安普・安普长老・欧熊・骷髅弓箭手
                    45130, 45046, 45092, 45138, // 骷髅斧手・小猎犬・侏儒战士・妖魔巡守
                    45098, 45127, 45143, 45149, // 甘地妖魔・罗孚妖魔・都达玛拉妖魔・阿吐巴妖魔
                    45171, 45040, 45155, 45192, // 那鲁加妖魔・熊・穴居人・鼠人
                    45173, 45213, 45079, 45144
            // 莱肯・卡司特・冰原狼人・蜥蜴人
            };
            final int rnd = Random.nextInt(mobArray.length);
            L1SpawnUtil.spawn(pc, mobArray[rnd], 0, 300000);

            final int itemId = useItem.getItemId();
            if ((itemId == 40006) || (itemId == 140006)) {
                useItem.setChargeCount(useItem.getChargeCount() - 1);
                pc.getInventory().updateItem(useItem,
                        L1PcInventory.COL_CHARGE_COUNT);
                if (useItem.getChargeCount() <= 0) { // 次数为 0时删除
                    pc.getInventory().removeItem(useItem, 1);
                }
            } else {
                pc.getInventory().removeItem(useItem, 1);
            }
        } else {
            PacketCreate.get().sendSinglePackets(pc, "没有任何事情发生");
        }
    }
}
